cmake_minimum_required(VERSION 2.8)
ADD_SUBDIRECTORY(exe)

# If this variable is defined, a pure cmake build is done end-to-end with no luarocks
IF (WITH_LUAROCKS STREQUAL OFF)

  # LUA_INCDIR - place where lua headers exist
  # LUA_LIBDIR - place where lua libraries exist
  # LUADIR - LUA_PATH
  # LIBDIR - LUA_CPATH
  # LUALIB - the lua library to link against

  IF (NOT DEFINED ${LUA_INCDIR})
     SET(LUA_INCDIR ${CMAKE_INSTALL_PREFIX}/include)
     MESSAGE(STATUS "LUA_INCDIR: ${LUA_INCDIR}")
  ENDIF(NOT DEFINED ${LUA_INCDIR})
  IF (NOT DEFINED ${LUA_LIBDIR})
     SET(LUA_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
     MESSAGE(STATUS "LUA_LIBDIR: ${LUA_LIBDIR}")
  ENDIF(NOT DEFINED ${LUA_LIBDIR})
  IF (NOT DEFINED ${LUA_BINDIR})
     SET(LUA_BINDIR ${CMAKE_INSTALL_PREFIX}/bin)
     MESSAGE(STATUS "LUA_BINDIR: ${LUA_BINDIR}")
  ENDIF(NOT DEFINED ${LUA_BINDIR})
  IF (NOT DEFINED ${LUALIB})
     IF (WITH_LUAJIT21 OR WITH_LUAJIT20)
       SET(LUALIB ${LIBRARY_OUTPUT_PATH}/libluajit.so)
     ELSE (WITH_LUAJIT21 OR WITH_LUAJIT20)
       SET(LUALIB ${LIBRARY_OUTPUT_PATH}/liblua.so)
     ENDIF (WITH_LUAJIT21 OR WITH_LUAJIT20)
     MESSAGE(STATUS "LUALIB: ${LUALIB}")
  ENDIF(NOT DEFINED ${LUALIB})
  IF (NOT DEFINED ${LUADIR})
     SET(LUADIR ${CMAKE_INSTALL_PREFIX}/${INSTALL_LUA_PATH_SUBDIR})
     MESSAGE(STATUS "LUADIR: ${LUADIR}")
  ENDIF(NOT DEFINED ${LUADIR})
  IF (NOT DEFINED ${LIBDIR})
     SET(LIBDIR ${CMAKE_INSTALL_PREFIX}/${INSTALL_LUA_CPATH_SUBDIR})
     MESSAGE(STATUS "LIBDIR: ${LIBDIR}")
  ENDIF(NOT DEFINED ${LIBDIR})
  IF (NOT DEFINED ${LUA_BINDIR})
    SET(LUA_BINDIR ${CMAKE_INSTALL_PREFIX}/bin)
    MESSAGE(STATUS "LUA_BINDIR: ${LUA_BINDIR}")
  ENDIF(NOT DEFINED ${LUA_BINDIR})
  IF (NOT DEFINED ${SCRIPTS_DIR})
    SET(SCRIPTS_DIR ${CMAKE_INSTALL_PREFIX}/bin)
    MESSAGE(STATUS "SCRIPTS_DIR: ${SCRIPTS_DIR}")
  ENDIF(NOT DEFINED ${SCRIPTS_DIR})

  INCLUDE_DIRECTORIES(${LUA_INCDIR})
  IF (WITH_LUAJIT21 OR WITH_LUAJIT20)
    INCLUDE_DIRECTORIES(${LuaJIT_SOURCE_DIR}/src ${LuaJIT_BINARY_DIR})
    SET(LUA "luajit")
  ELSE (WITH_LUAJIT21 OR WITH_LUAJIT20)
    INCLUDE_DIRECTORIES(${Lua_SOURCE_DIR}/src ${Lua_BINARY_DIR})
    SET(LUA "lua")
  ENDIF (WITH_LUAJIT21 OR WITH_LUAJIT20)

  # add torch defs
  # this is done to fake compliance with FIND_PACKAGE(Torch)
  SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake" "${CMAKE_MODULE_PATH}")

  INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/pkg/torch/lib/TH")
  INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}/pkg/torch/lib/TH")
  INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/pkg/torch/lib/luaT")
  INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}/pkg/torch/lib/luaT")

  SET(Torch_INSTALL_INCLUDE_SUBDIR ${LUA_INCDIR})
  SET(Torch_INSTALL_INCLUDE ${LUA_INCDIR})
  SET(Torch_INSTALL_LUA_PATH_SUBDIR ${LUADIR})
  SET(Torch_INSTALL_LUA_CPATH_SUBDIR ${LIBDIR})
  SET(Torch_INSTALL_LIB ${LUA_LIBDIR})
  SET(Torch_INSTALL_LIB_SUBDIR ${LUA_LIBDIR})

  # Finally add the two directories
  ADD_SUBDIRECTORY(pkg)
  ADD_SUBDIRECTORY(extra)

ENDIF (WITH_LUAROCKS STREQUAL OFF)
